<template>
  <div class="content" :class="{ content_collse: isCollse }">
    <!-- TAG 标签 -->
    <div class="tag" v-if="showTag">
      <ul class="ul" >
        <li
          v-for="(item, index) in tagList"
          :key="index"
          :class="{ 'clickColor': isActive(item.path)}"
        >
          <router-link :to="item.path" >{{ $t(`i18n.${item.name}`) }}</router-link>
          <span class="error" @click="close(index)"
            ><i class="el-icon-error"></i
          ></span>
        </li>
      </ul>
      <div class="dropdown">
        <el-dropdown size="mini" type="primary">
          <el-button type="primary" size="mini">
            标签选择<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            
            <el-dropdown-item @click.native="closeAll">关闭所有</el-dropdown-item>
            <el-dropdown-item @click.native="closeOther">关闭其他</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  methods: {

    // 关闭所有
    closeAll(){
  
        this.tagList=[]
        this.$router.push('/')
    },

    // 关闭其他
    closeOther(){
        console.log(this.$route.fullPath);
     this.tagList =this.tagList.filter(value=>{
       return value.path === this.$route.fullPath
     })
    },

    // 关闭按钮
    close(index) {
        this.tagList.splice(index,1)
        var item =this.tagList[index]?this.tagList[index]:this.tagList[index-1]
        if(item){
              this.$router.push(item.path)
        }else{
           this.$router.push('/')
        }
    },

    // 是否选择
    isActive(path){
      // this.$route.fullPath即当前的路由路径
      
      return path ===this.$route.fullPath
    },

    //  获取并添加  路由信息
    setTabList(route){
        console.log(route);
        const isExist = this.tagList.some(value=>{
     
            return value.path ===route.path
        })
        console.log(!isExist);
        if(!isExist){
            if(this.tagList.length>=8){
              this.tagList.shift()
              }
              this.tagList.push({
                path:route.path,
                name:route.name
              })
            }
        }
        
    
  },
  data() {
    return {
      isCollse: true,
      tagList: []
    };
  },
  watch:{
      $route(newValue,oldValue){
        console.log(oldValue);
          this.setTabList(newValue)
      }
  },
 
  beforeCreate() {
    /* 展开 */
    this.$bus.$on("open", () => {
      this.isCollse = false;
    });
    /* 折叠 */
    this.$bus.$on("close", () => {
      this.isCollse = true;
    });
  },
  created() {
    console.log(this.$route);
  this.setTabList(this.$route)
    
  },
  computed:{
    showTag(){
        return  this.tagList.length>0
    }
  }
};
</script>

<style scoped>
.error {
  margin: 0 5px;
}
.ul {
  user-select: none;

  padding: 0;
  margin: 0;
  display: inline-block;
  width: 900px;
  overflow: hidden;
}
li {
  display: inline-block;
  background-color: #409eff;
  height: 30px;
  line-height: 30px;
  margin: 2px 5px;
  padding: 0 5px;
  color: white;
  border-radius: 10px;
  font-size: 12px;
}
li a {
  color: white;
  text-decoration: none;
}
.clickColor {
  background-color: #7700ff;
}
.content {
  position: absolute;
  top: 80px;
  right: 0;
  left: 250px;
  bottom: 0;
  background-color: #f0f0f0;
  transition: 0.3s;
  padding: 10px;
  overflow-y: scroll;
}
.tag {
  height: 40px;
  line-height: 40px;
  position: sticky;
  background-color: #fff;
  margin-bottom: 10px;
  z-index: 10;
  overflow: hidden;
  top: 0;
  left: 0;
}
.dropdown {
  float: right;
  position: absolute;
  width: 100px;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
}

.content_collse {
  left: 70px;
  transition: 0.3s;
}
</style>